package com.tj.collection.controller.stats.response;

import com.alibaba.fastjson.JSONArray;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.Serializable;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2017/3/24
 * Time: 下午5:10
 */
public class StatsResponse implements Serializable {
    private static final long serialVersionUID = -7890445354147715157L;
    private List<String> keyList = new ArrayList<>();
    private List<Object> data = new JSONArray();
    private List<Map<String, Object>> exportdData = new ArrayList<>();

    public StatsResponse(List<Object> result) {
        Map<String, Object> columnMap = (Map<String, Object>) result.get(0);
        for (Object object : result) {
            Map<String, Object> map = (Map<String, Object>) object;
            if (map.size() > columnMap.size()) {
                columnMap = map;
            }
        }
        Map<String, String> orderKeyMap = new HashMap<>();

        List<String> keysOrder = new ArrayList<>();

        for (String key : columnMap.keySet()) {
            if (key.contains("#")) {
                String[] keyArrayTemp = key.split("#");
                orderKeyMap.put(keyArrayTemp[1], keyArrayTemp[0]);
                keysOrder.add(keyArrayTemp[1]);
            }
        }

        Collections.sort(keysOrder, new Comparator<String>() {
            public int compare(String a, String b) {
                Integer f = Integer.parseInt(a);
                Integer e = Integer.parseInt(b);
                return f.compareTo(e);
            }
        });

        for (String orderedKey : keysOrder) {
            keyList.add(orderKeyMap.get(orderedKey));
        }

        for (Object eachData : result) {
            Map<String, Object> eachDataMap = (Map<String, Object>) eachData;
            Map<String, Object> temp = new HashMap<>();
            for (String k : eachDataMap.keySet()) {
                String[] keyArrayTemp = k.split("#");
                temp.put(keyArrayTemp[0], eachDataMap.get(k));
            }

            data.add(temp);
            exportdData.add(temp);
        }
    }

    public List<String> getKeyList() {
        return keyList;
    }

    public StatsResponse setKeyList(List<String> keyList) {
        this.keyList = keyList;
        return this;
    }

    public List<Object> getData() {
        return data;
    }

    public StatsResponse setData(List<Object> data) {
        this.data = data;
        return this;
    }


    private Workbook workbook;

    public Sheet initWorkbook() {
        workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("person efficiency statistics");
        Row firstRow = sheet.createRow(0);
        for (int i = 0; i < keyList.size(); i++) {
            firstRow.createCell(i).setCellValue(keyList.get(i));
        }
        return sheet;
    }


    /**
     * 组装导出excel表的数据
     *
     * @param sheet
     */
    public void makeSheet(Sheet sheet) {
        for (int i = 0; i < exportdData.size(); i++) {
            Map<String, Object> eachDataMap = exportdData.get(i);
            Row row = sheet.createRow(i + 1);
            for (int j = 0; j < keyList.size(); j++) {
                String key = keyList.get(j);
                String value = eachDataMap.get(key).toString();
                row.createCell(j).setCellValue(value);
            }
        }
    }


    public Workbook getWorkbook() {
        return workbook;
    }

    public StatsResponse setWorkbook(Workbook workbook) {
        this.workbook = workbook;
        return this;
    }
}
